package org.aseclipse.core.runtime.app
{
    import mx.core.Application;
    
    import org.aseclipse.core.runtime.registry.IConfigurationElement;
    /****
    *    <application
               label="Modeling Workbench"
               id="Modeling">
            <url pattern="modeling">
            </url>
            <entry id="login" class="org.aseclipse.ui.login">
            	<parameter name="next" value="org.aseclipse.app.home"/>
            </entry>
         </application>
    * 
    */ 
    public class AppDescriptor
    {
        private var id_:String;
        private var element_:IConfigurationElement;
        private var pattern_:String;
        private var entry:String;
        
        public function AppDescriptor(id:String, ele:IConfigurationElement)
        {
            id_ = id;
            element_ = ele;
            var array:Array = ele.getChildrenByName("url");
            if(array && array.length > 0) {
                pattern_ = (array[0] as IConfigurationElement).getAttribute("pattern");
                if(pattern_.indexOf("/") == 0)
                    pattern_ = pattern_.substring(1, pattern_.length);
            } 
        }
        
        public function getID():String {
            return id_;
        }
        
        public function getLabel():String {
            return element_.getAttribute("label");
        }
        
        public function match():Boolean {
            if(pattern_ !=  null) {
                var url:String = Application.application.url;
                // pattern looks like "osgi/Modeling" matches url "http://.../osgi/Modeling"
                if(url.indexOf(pattern_) != -1) 
                    return true;
                var para:Object = Application.application.parameters; 
                // pattern looks like "app=Modeling" matches url "http://.../Main.swf?app=Modeling"
                if(pattern_.indexOf("=") != -1) {
                    var inputs:Array = pattern_.split("=");    
                    if(para[inputs[0]] == inputs[1]) 
                        return true;
                } else {
                    // pattern looks like "Modeling" matches url "http://.../Main.swf?Modeling"
                    if(para[pattern_] != null)
                        return true;    
                }
            }
            return false;
        }

        public function startOn(context:IApplicationContext):void {
            trace("[startOn] "+ getID());
            var array:Array = element_.getChildrenByName("entry");
            if(array && array.length > 0) {
                (array[0] as IConfigurationElement).createExecutableExtension("class", function(obj:Object):void {
                    (obj as IApplication).start(context);
                });
            }
        }
    }
}